{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6de5d609",
   "metadata": {},
   "source": [
    "## numpy数组的索引和切片方法\n",
    "\n",
    "ndarrays可以使用标准Python x[obj]语法对其进行索引 ，其中 x 是数组， obj 是选择。有三种可用的索引：字段访问，基本切片，高级索引。哪一个发生取决于 obj "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "51ec6ad4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "54e41187",
   "metadata": {},
   "outputs": [],
   "source": [
    "a1 = np.array([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84edb1bb",
   "metadata": {},
   "source": [
    "##  1.一维数组索引的方法，同python的列表一致"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9e1dcb04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "[2 3 4]\n"
     ]
    }
   ],
   "source": [
    "print(a1[0])\n",
    "print(a1[1:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "849968c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 5])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取数组中多个不连续数据。python列表没有获取多个不连续数据的方法。\n",
    "a1[[1,4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ea031cf",
   "metadata": {},
   "source": [
    "## 2. 多维数组的索引和切片\n",
    "也是通过中括号进行索引和切片。在中括号中使用逗号分隔，逗号前面是行，逗号后面是列；\n",
    "如果是多维数组中只有一个值，那么这个值就是行。获取列数据的化，逗号前面加:,后面是列号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "937b8283",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[8 8 9 2 3 4]\n",
      " [1 3 4 8 6 9]\n",
      " [2 9 3 6 1 7]\n",
      " [9 0 8 6 7 7]]\n"
     ]
    }
   ],
   "source": [
    "a2 = np.random.randint(0,10,size=(4,6))\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a4697060",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 6, 7, 2, 9, 9])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取二维数组的某一行\n",
    "a2[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "372ba80d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 9, 0, 7, 6, 7],\n",
       "       [6, 4, 2, 8, 3, 3]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取二维数组的某几行（多行连续）\n",
    "a2[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1b05aea9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 6, 7, 2, 9, 9],\n",
       "       [4, 9, 0, 7, 6, 7],\n",
       "       [6, 4, 2, 8, 3, 3]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取二维数组多行不连续\n",
    "a2[[0,1,2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e2f948a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#此处和python的列表索引存在区别。使用,号后，numpy的索引较为简洁。\n",
    "#一级[]中的,区分行列。行内、列内用[]包括即可。\n",
    "a2[1,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5d1d2118",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 3])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#多行多列切片,不连续\n",
    "a2[[1,2],[4,5]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1aae872e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 6],\n",
       "       [8, 3]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#多行多列切片，连续\n",
    "a2[1:3,3:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7159a268",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7 2]\n",
      "[[7 6]\n",
      " [8 3]]\n"
     ]
    }
   ],
   "source": [
    "#多行多列，连续不连续的情况对比\n",
    "print(a2[[1,3],[3,5]])\n",
    "print(a2[1:3,3:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9fe67193",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 4, 3, 8])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#当获取整列时，行位置只填写:表示全选\n",
    "a2[:,2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcca7211",
   "metadata": {},
   "source": [
    "## 3.numpy 布尔索引\n",
    "\n",
    "用于数字提取很方便。理论上，布尔索引是一种高级索引，与基本索引和切片返回的视图不同，布尔索引直接返回新的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a40c6459",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "a3 = np.arange(24).reshape((4,6))\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "63295ce2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3[a3>10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "529f630c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "source": [
    "d = a3[a3>5]\n",
    "print(d)\n",
    "d[3]=99\n",
    "print('*'*20)\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b81e1725",
   "metadata": {},
   "source": [
    "可以使用多种条件形成布尔值,可以用&代表且，|代表或。多个条件用()括起来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "070b66c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4, 16, 17, 18, 19, 20, 21, 22, 23])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取<5,>10的值\n",
    "a3[(a3<5)|(a3>15)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17176dd6",
   "metadata": {},
   "source": [
    "## 4.使用函数替换数组中的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "60e557e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 0 7 6 0]\n",
      " [5 8 7 4 1]\n",
      " [1 5 0 9 5]]\n"
     ]
    }
   ],
   "source": [
    "a4 = np.random.randint(0,10,size=(3,5))\n",
    "print(a4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "abfb55a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 1 1 4 2]\n",
      " [0 0 0 0 0]\n",
      " [6 2 1 4 5]]\n"
     ]
    }
   ],
   "source": [
    "#将某一行的数据替换为固定数据\n",
    "a4[1]=0\n",
    "print(a4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "f02b5ac3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 1 1 4 2]\n",
      " [1 2 3 4 5]\n",
      " [6 2 1 4 5]]\n"
     ]
    }
   ],
   "source": [
    "#将某一行替换为固定的数组\n",
    "a4[1]=[1,2,3,4,5]\n",
    "print(a4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "a38de6ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 4 0]\n",
      " [0 0 3 4 5]\n",
      " [6 0 0 4 5]]\n"
     ]
    }
   ],
   "source": [
    "#使用条件索引替换，例如小于3的数据全部替换为0\n",
    "a4[a4<3]=0\n",
    "print(a4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7b8a8fb",
   "metadata": {},
   "source": [
    "## 5. np.where函数的使用\n",
    "\n",
    "where(condition, [x, y])：\n",
    "\n",
    "Parameters\n",
    "    ----------\n",
    "    condition : array_like, bool\n",
    "        Where True, yield `x`, otherwise yield `y`.x, y : array_like\n",
    "        Values from which to choose. `x`, `y` and `condition` need to bebroadcastable to some shape.\n",
    "    \n",
    "    Returns\n",
    "    -------\n",
    "    out : ndarray。返回一个新的ndarray数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "48705a9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 1 1 0]\n",
      " [1 1 1 0 0]\n",
      " [0 1 0 1 1]]\n"
     ]
    }
   ],
   "source": [
    "#使用where函数定位，在输入两个参数，符合条件的变为的值和不符合条件的变为的值。\n",
    "result = np.where(a4<5,0,1)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "594ec673",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 0, 7, 6, 0],\n",
       "       [5, 8, 7, 4, 1],\n",
       "       [1, 5, 0, 9, 5]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "28d98bc8",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function where in module numpy:\n",
      "\n",
      "where(...)\n",
      "    where(condition, [x, y])\n",
      "    \n",
      "    Return elements chosen from `x` or `y` depending on `condition`.\n",
      "    \n",
      "    .. note::\n",
      "        When only `condition` is provided, this function is a shorthand for\n",
      "        ``np.asarray(condition).nonzero()``. Using `nonzero` directly should be\n",
      "        preferred, as it behaves correctly for subclasses. The rest of this\n",
      "        documentation covers only the case where all three arguments are\n",
      "        provided.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    condition : array_like, bool\n",
      "        Where True, yield `x`, otherwise yield `y`.\n",
      "    x, y : array_like\n",
      "        Values from which to choose. `x`, `y` and `condition` need to be\n",
      "        broadcastable to some shape.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : ndarray\n",
      "        An array with elements from `x` where `condition` is True, and elements\n",
      "        from `y` elsewhere.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    choose\n",
      "    nonzero : The function that is called when x and y are omitted\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    If all the arrays are 1-D, `where` is equivalent to::\n",
      "    \n",
      "        [xv if c else yv\n",
      "         for c, xv, yv in zip(condition, x, y)]\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> a = np.arange(10)\n",
      "    >>> a\n",
      "    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
      "    >>> np.where(a < 5, a, 10*a)\n",
      "    array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])\n",
      "    \n",
      "    This can be used on multidimensional arrays too:\n",
      "    \n",
      "    >>> np.where([[True, False], [True, True]],\n",
      "    ...          [[1, 2], [3, 4]],\n",
      "    ...          [[9, 8], [7, 6]])\n",
      "    array([[1, 8],\n",
      "           [3, 4]])\n",
      "    \n",
      "    The shapes of x, y, and the condition are broadcast together:\n",
      "    \n",
      "    >>> x, y = np.ogrid[:3, :4]\n",
      "    >>> np.where(x < y, x, 10 + y)  # both x and 10+y are broadcast\n",
      "    array([[10,  0,  0,  0],\n",
      "           [10, 11,  1,  1],\n",
      "           [10, 11, 12,  2]])\n",
      "    \n",
      "    >>> a = np.array([[0, 1, 2],\n",
      "    ...               [0, 2, 4],\n",
      "    ...               [0, 3, 6]])\n",
      "    >>> np.where(a < 4, a, -1)  # -1 is broadcast\n",
      "    array([[ 0,  1,  2],\n",
      "           [ 0,  2, -1],\n",
      "           [ 0,  3, -1]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.where)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
